package com.tyqx.hashchainbackend.service.impl;


import com.tyqx.hashchainbackend.service.SysRoleService;
import com.tyqx.hashchainbean.module.SysRole;
import com.tyqx.hashchainbean.vo.SysRoleVO;
import com.tyqx.hashchaindao.mapper.SysRoleMapper;
import hashchainutil.common.BackendPage;
import hashchainutil.common.Constants;
import hashchainutil.common.EmptyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;

    public SysRole getById(Long roleId) throws Exception {
        return sysRoleMapper.getById(roleId);
    }

    public List<SysRole> getListByMap(Map<String, Object> param) throws Exception {
        return sysRoleMapper.getListByMap(param);
    }

    public Integer getCountByMap(Map<String, Object> param) throws Exception {
        return sysRoleMapper.getCountByMap(param);
    }

    public boolean save(SysRole sysRole) throws Exception {
        sysRole.setRoleCreationDate(new Date());
        return sysRoleMapper.save(sysRole) > 0 ? true : false;
    }

    public boolean modify(SysRole sysRole) throws Exception {
        sysRole.setRoleModifyDate(new Date());
        return sysRoleMapper.modify(sysRole) > 0 ? true : false;
    }

    public Integer removeById(Long roleId) throws Exception {
        return sysRoleMapper.removeById(roleId);
    }

    public BackendPage<List<SysRole>> queryPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = sysRoleMapper.getCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        param.put("beginPos", (pageNo - 1) * pageSize);
        param.put("pageSize", pageSize);
        List<SysRole> sysRoleList = sysRoleMapper.getListByMap(param);
        BackendPage page = new BackendPage(total, sysRoleList);
        return page;
    }

    @Override
    public List<SysRole> queryRoleKey(Long roleId) {
        return sysRoleMapper.getRoleKeyById(roleId);
    }

    @Override
    public List<SysRoleVO> getAllByUserId(Long userId) {

        return sysRoleMapper.getAllByUserId(userId);
    }

}
